//
//  ListPikerController.m
//  CarTown
//
//  Created by xionghao on 15/9/9.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ListPikerController.h"
#import "EidtViewController.h"
#import "CityPickerView.h"
@interface ListPikerController ()<EidtViewControllerDelegate,CityPickerDelegate>
@property (nonatomic,assign) NSInteger oldpostion;
@property (nonatomic,strong) CityPickerView *pickerView;
@end

@implementation ListPikerController
+(instancetype)viewcontroller{
    ListPikerController *vc=[[ListPikerController alloc]initWithStyle:UITableViewStyleGrouped];
    return vc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor=bgGarycolor;
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 16;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.iscanEidt||self.iscanSelect) {
        return 2;
    }else{
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (section==0) {
        return self.dataarray.count;
    }else{
        return 1;
    }
    
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static  NSString *ID=@"pikercell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.textLabel.font=[UIFont systemFontOfSize:14];
        cell.textLabel.textColor=textnormalGary;
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
    
    if (indexPath.section==0) {
    if (indexPath.row==self.currtpostion) {
        cell.textLabel.textColor=tintblue;
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    }else{
         cell.accessoryType=UITableViewCellAccessoryNone;
        cell.textLabel.textColor=textnormalGary;
    }
    
     cell.textLabel.text=self.dataarray[indexPath.row];
    }else{
        if (self.iscanEidt) {
            cell.textLabel.text=[NSString stringWithFormat:@"自定义%@",self.navigationItem.title];
        }else if (self.iscanSelect){
         cell.textLabel.text=[NSString stringWithFormat:@"选择城市"];
        }
    }
    // Configure the cell...
    
    return cell;
}
-(void)citypicker:(NSString *)city{
    if ([_delegate respondsToSelector:@selector(ListPickerDelegate:WithCumerString:)]) {
        [_delegate ListPickerDelegate:self WithCumerString:city];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(CityPickerView *)pickerView{
    if (_pickerView==nil) {
        _pickerView=[[CityPickerView alloc]initWithDelegate:self WithColoum:2];
    }
    return _pickerView;
}
-(void)showpicker{
    [self.pickerView showinView:self.navigationController.view];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1) {
        if (self.iscanEidt) {
            [self showeidt];
        }else{
            [self showpicker];
        }
        
    }else{
    self.oldpostion=self.currtpostion;
    self.currtpostion=indexPath.row;
        [self.tableView reloadData];
        if ([_delegate respondsToSelector:@selector(ListPickerDelegate:WithIndex:)]) {
            [_delegate ListPickerDelegate:self WithIndex:self.currtpostion];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    
    }
    
}
-(void)showeidt{
   EidtViewController *vc= [EidtViewController viewcontroller];
    vc.titletxt=[NSString stringWithFormat:@"自定义%@",self.navigationItem.title];
    vc.delegate=self;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)EidtViewControllerClick:(NSString *)text{
    if ([_delegate respondsToSelector:@selector(ListPickerDelegate:WithCumerString:)]) {
        [_delegate ListPickerDelegate:self WithCumerString:text];
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
